package com.qc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qc.dto.Result;
import com.qc.enity.Goods;
import com.qc.exception.ResultCodeEnum;
import com.qc.mapper.GoodsMapper;
import com.qc.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;



/**
 * 冰箱物品控制层
 */

@RestController
@RequestMapping("goods")
@CrossOrigin
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private GoodsMapper goodsMapper;
    /** 菜吃了多少 设置百分比 根据goodsid*/
    @PostMapping("baifenbi")
    public Result<Integer> baifenbi (@RequestParam("baifenbi") String baifenbi ,
                                     @RequestParam("goodsId") String goodsId) {
        Goods goods = new Goods();
        goods.setGoodsId(goodsId);
        int i = goodsMapper.updateById(goods);
        if (i == 1 ){
            return Result.successResultOnlyMessage(ResultCodeEnum.SUCCESS.getMsg());
        }
        //添加失败
        return Result.error(ResultCodeEnum.GOODS_ADD_FAIL.getCode(), ResultCodeEnum.GOODS_ADD_FAIL.getMsg());
    }

    /** 获取食材列表 */
    @GetMapping("list/{oid}/{current}/{size}")
    public Result<IPage<Goods>> goodsList(@PathVariable String oid, @PathVariable int current, @PathVariable int size){

        //设置分页条件
        Page page = new Page(current, size);
        IPage<Goods> data = goodsService.goodsList(oid, page);


        //如果data为空则表示goods已全部展示
        if (data == null){
            return Result.error(ResultCodeEnum.GOODS_OVER.getCode(), ResultCodeEnum.GOODS_OVER.getMsg());
        }

        return Result.successResult(data);
    }

    /** 根据userId模糊查询食材 */
    @GetMapping("select/{oid}/{current}/{size}/{name}")
    public Result<IPage<Goods>> goodsSelect(@PathVariable String oid, @PathVariable int current, @PathVariable int size, @PathVariable String name){

        //设置分页条件
        Page page = new Page(current, size);
        IPage<Goods> data = goodsService.goodsSelect(oid, page, name);

        //如果data为空则表示goods已全部展示
        if (data == null){
            return Result.error(ResultCodeEnum.GOODS_OVER.getCode(), ResultCodeEnum.GOODS_OVER.getMsg());
        }

        return Result.successResult(data);
    }
    @GetMapping("add/{oid}/{name}/{num}/{goodsDescribe}/{expiration}/{unit}/{type}")
    public Result<Void> addGoods(@PathVariable String oid, @PathVariable String name, @PathVariable Integer num, @PathVariable String goodsDescribe, @PathVariable Date expiration, @PathVariable String unit, @PathVariable String type , @RequestParam("photoUrl") String photoUrl) {

        Integer rows = goodsService.addGoods(oid, name, num, goodsDescribe, expiration, unit, type ,photoUrl );

        if (rows != 1){
            //添加失败
            return Result.error(ResultCodeEnum.GOODS_ADD_FAIL.getCode(), ResultCodeEnum.GOODS_ADD_FAIL.getMsg());
        }

        return Result.successResultOnlyMessage(ResultCodeEnum.SUCCESS.getMsg());

    }

    @GetMapping("remove/{oid}/{goods_id}/{num}")
    public Result<Void> removeGoods(@PathVariable String oid,@PathVariable String goodsId,@PathVariable Integer num){

        Integer rows = goodsService.removeGoods(oid, goodsId, num);

        if (rows != 1){
            //添加失败
            return Result.error(ResultCodeEnum.GOODS_REMOVE_FAIL.getCode(), ResultCodeEnum.GOODS_REMOVE_FAIL.getMsg());
        }

        return Result.successResultOnlyMessage(ResultCodeEnum.SUCCESS.getMsg());

    }

    /**
     * yolo小开关
     * 通过yolo添加食物
     * 我有了开门的动作之后 关门了进行识别食物 上传
     */
    @GetMapping("open/{userId}")
    public Result<Void> removeGoods(@PathVariable String userId){
        int rows = goodsService.addGoodsByYoLo(userId);
        if (rows != 1){
            //添加失败
            return Result.error(ResultCodeEnum.GOODS_REMOVE_FAIL.getCode(), ResultCodeEnum.GOODS_REMOVE_FAIL.getMsg());
        }
        return Result.successResultOnlyMessage(ResultCodeEnum.SUCCESS.getMsg());

    }




}
